source("./code/loadPackages.R") # Install and load all necessary packages

# Load in dictionary-based data
mad = fread("./data/dictionaryData.csv")


#### Table A40: Descriptive Statistics for Topic Propensities ####
summary(mad$propViolence)
summary(mad$propThreat)
summary(mad$propBalance)
summary(mad$propDiplo)
summary(mad$propAdv)


# Regressions
violence_all = lm(propViolence ~ hawkMean + formal + I(doctype=="Transcript") + factor(admin), data=mad)

threat_all = lm(propThreat ~ hawkMean + formal + I(doctype=="Transcript") + factor(admin), data=mad)

balance_all = lm(propBalance ~ hawkMean + formal + I(doctype=="Transcript") + factor(admin), data=mad)

diplo_all = lm(propDiplo ~ hawkMean + formal + I(doctype=="Transcript") + factor(admin), data=mad)

adversary_all = lm(propAdv ~ hawkMean + formal + I(doctype=="Transcript") + factor(admin), data=mad)


#### Table A41: Speaker Hawkishness and Speech Act Content ####
stargazer(violence_all, threat_all, balance_all, diplo_all, adversary_all,
          no.space=T,
          align=T,omit.stat=c("f","ser","adj.rsq","rsq"),digits=4,
          covariate.labels = c("Speaker Hawkishness", "Formal", "Transcript", "Eisenhower",
                               "Kennedy", "Johnson", "Nixon", "Ford", "Carter", "Reagan"))

models = c("violence_all", "threat_all", "balance_all", "diplo_all", "adversary_all")



#### Figure 6: Effect of Adviser Hawkishness on Topic Proportions During Meetings ####

# Gather data to produce coefficient plot
hawkCoef = hawkSE = NA
for (i in 1:length(models)) {
  onemodel = get(models[i])
  hawkCoef[i] = summary(onemodel)$coefficients["hawkMean","Estimate"]
  hawkSE[i] = summary(onemodel)$coefficients["hawkMean","Std. Error"]
}
plotdata = data.frame(model=models, hawkCoef, hawkSE)
plotdata$lower = plotdata$hawkCoef - 1.96*hawkSE
plotdata$upper = plotdata$hawkCoef + 1.96*hawkSE
plotdata$model = factor(plotdata$model, levels=models)
plotdata$signif = ifelse((plotdata$hawkCoef > 0 & plotdata$lower > 0) | (plotdata$hawkCoef < 0 & plotdata$upper < 0), "Yes", "No")

# Make coefficient plot
topicPlot = ggplot(plotdata, aes(model, hawkCoef)) + geom_pointrange(aes(ymin=lower, ymax=upper, shape=signif)) +
  geom_hline(yintercept=0, linetype=2) +
  scale_x_discrete("", labels=c("Violence", "Threat", "Military\nBalance", "Diplomacy", "Adversary\nInterests")) +
  scale_y_continuous("Effect of speaker's hawkishness\non topic proportion") + theme_bw() + 
  scale_shape_manual("95% Significance", values=c(15,15)) + theme(legend.position = "none")

print(topicPlot)

ggsave(filename="./figures/dictionary_advMtg.pdf", plot=topicPlot,
       height=3, width=4.25, units='in')

# Clear environment
rm(list = ls())
